<?php

namespace oa\controllers;

use Yii;
use oa\services\DeliveryRecordService;
use oa\services\DeliveryCompanyService;

class DeliveryController extends BaseController
{

	public function actionIndex()
	{
        if ($this->request->isAjax) {
            return DeliveryRecordService::getList();
        }
        return $this->fetch();
	}

	public function actionAdd()
	{
		if ($this->request->isPost) {
			$res = DeliveryRecordService::save();
			if ($res === true) {
				return $this->ok('提交成功');
			}
			return $this->err('提交失败', $res);
        }
        $cid = $this->input('cid');
		return $this->fetch([
			'companies' => DeliveryCompanyService::getAll(),
			'cid' 		=> $cid
		], 2);
	}

	public function actionEdit()
	{
		$id = $this->input('id');
		if ($this->request->isPost) {
			$res = DeliveryRecordService::save($id);
			if ($res === true) {
				return $this->ok('提交成功');
			}
			return $this->err($res);
        }
		$data = DeliveryRecordService::one($id, 1);
		return $this->fetch([
			'companies' => DeliveryCompanyService::getAll(),
			'data' => $data
		], 2);
	}

	public function actionRemove()
	{
		if ($this->request->isAjax) {
			if (DeliveryRecordService::remove($this->input('id'))) {
				return $this->ok('删除成功');
			}
			return $this->err('删除失败');
        }
	}

	public function actionAudit()
	{
		if ($this->request->isAjax) {
            return DeliveryRecordService::getAuditList();
        }
        return $this->fetch();
	}

	public function actionReject()
	{
		if ($this->request->isAjax) {
			if (DeliveryRecordService::reject($this->input('id'), $this->input('audit_msg'))) {
				return $this->ok('审核成功');
			}
			return $this->err('审核失败');
        }
	}

	public function actionApprove()
	{
		if ($this->request->isAjax) {
			if (DeliveryRecordService::approve($this->input('id'))) {
				return $this->ok('审核成功');
			}
			return $this->err('审核失败');
        }
	}


}